<?php

/**
 * @Author: 杰少Pakey
 * @Email : admin@ptcms.com
 * @File  : archive.php
 */
abstract class archiveController extends AdminController
{
	protected $module;

	public function init()
	{
		$this->tableName = 'archive';
		parent::init();
	}

	public function indexAction()
	{
		$this->map['module'] = $this->module;
		$this->getMap();
		$this->_list();
		cookie('admin_listurl', __SELF__);
		$this->display('archive_index');
	}

    public function _data($list)
    {
        foreach($list as &$data){
            $data['url']=$this->model->geturl($data['module'],$data['key'],$data['category_id']);
        }
        return $list;
    }

	public function addAction()
	{
		$this->assign('actionname', '添加' . $this->typename);
		$this->assign('categorylist', M('category')->getModuleList($this->module));
		parent::addAction();
	}

	public function insertAction()
	{
        //PT单本小说程序继承了PT程序一贯以来的简单、智能、自动化的特点，努力为运营单本小说程序站点的站长朋友们提供大力的支持。当然，程序拿到手就是要安装了，现在就让我们一步一步来指引大家安装这个程序。

		$archiveData = $this->getArchiveData();
        //$this->model->uploadcontent(1,$archiveData['litpic']);exit;
		//插入主表
		if ($id = $this->model->insert($archiveData)) {
			//如果没有设置key 那么自动设置key为archive id
			if (empty($archiveData['key'])) {
				$this->model->where('id=' . $id)->update(array('key' => $id));
			}
            $this->model->uploadcontent($id,$archiveData['litpic']);
            $addonData = $this->getAddonData();
			$addonData['archive_id'] = $id;
			if (false !== M($this->module)->insert($addonData)) {
				$this->success('添加成功', cookie('admin_listurl'));
			}
		}
		$this->error('添加失败', cookie('admin_listurl'));
	}

	public function editAction()
	{
		$this->assign('actionname', '修改' . $this->typename);
		$this->assign('categorylist', M('category')->getModuleList($this->module));
		$id = I('get.id');
		if ($id) {
			if ($info = $this->model->getinfo($id)) {
				$this->assign('info', $info);
				$this->display();
				exit;
			}
		}
		$this->error('未找到对应的' . $this->typename);
	}

	public function updateAction()
	{
		$archiveData=$this->getArchiveData();
		$addonData = $this->getAddonData();
		$id=$_POST['id'];
		//插入主表
		if (false  !== $this->model->where(array('id'=>$id))->update($archiveData)) {
			if (false !== M($this->module)->where(array('archive_id'=>$id))->update($addonData)) {
				//修改成功后更新相应缓存
				$m=$this->module;
				dc::$m($id,true);
				$this->success('修改成功', cookie('admin_listurl'));
			}
		}
		$this->error('修改失败', cookie('admin_listurl'));
	}

	protected function getArchiveData()
	{
		return array(
			'category_id' => $_POST['cid'],
			'push_category_id' => isset($_POST['push_category_id'])?$_POST['push_category_id']:0,
			'module' => $this->module,
			'title' => $_POST['title'],
			'caption' => $_POST['caption'],
			'post_user_id' => $this->userid,
			'posttime' => strtotime($_POST['posttime']),
			'updatetime' => strtotime($_POST['updatetime']),
			'litpic' => $_POST['litpic'],
			'viewnum' => $_POST['viewnum'],
			'commentnum' => $_POST['commentnum'],
			'marknum' => $_POST['marknum'],
			'top' => $_POST['top'],
			'good' => $_POST['good'],
			'keywords' => $_POST['keywords'],
			'description' => empty($_POST['description'])?truncate(clearformat($_POST['content']),200):clearformat($_POST['description']),
			'key' => $_POST['key'],
			'status' => $_POST['status'],
		);
	}

	abstract public function getAddonData();
}